import type { VbenFormSchema } from '#/adapter/form';
import type {
  OnActionClickParams,
  VxeTableGridOptions,
} from '#/adapter/vxe-table';
import type { ProductApi } from '#/models/product/product';

import { z } from '#/adapter/form';
import { dictFormatter, useDictOptions } from '#/hooks/useDictOptions';
import { $t } from '#/locales';
import { op } from '#/utils/permission';
/**
 * 搜索表单配置
 * @description 用于产品列表页的搜索表单字段配置
 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'sku',
      label: 'SKU',
      componentProps: {
        placeholder: '请输入SKU',
        allowClear: true,
      },
    },
    {
      component: 'Input',
      fieldName: 'name',
      label: '货品名称',
      componentProps: {
        placeholder: '请输入货品名称',
        allowClear: true,
      },
    },
    {
      component: 'Input',
      fieldName: 'spc',
      label: '货品规格',
      componentProps: {
        placeholder: '请输入货品规格',
        allowClear: true,
      },
    },
    {
      component: 'Select',
      fieldName: 'status',
      label: '货品状态',
      componentProps: {
        placeholder: '请选择货品状态',
        allowClear: true,
        options: useDictOptions('product_status'),
      },
    },
  ];
}

// region 创建和编辑表单
/**
 * 创建和编辑表单配置
 * @description 用于产品创建和编辑表单的字段配置
 */
export function useSchema(): VbenFormSchema[] {
  return [
    // 基础信息字段
    {
      component: 'Input',
      fieldName: 'sku',
      label: 'SKU',
      componentProps: {
        placeholder: '请输入SKU',
        class: 'w-1/2',
      },
      rules: z
        .string()
        .min(1, $t('ui.formRules.required', ['SKU']))
        .max(50, $t('ui.formRules.maxLength', ['SKU', 50])),
    },
    {
      component: 'Input',
      fieldName: 'sku_supplier',
      label: '供应商SKU',
      componentProps: {
        placeholder: '请输入供应商SKU',
        class: 'w-1/2',
      },
      rules: z
        .string()
        .max(64, $t('ui.formRules.maxLength', ['供应商SKU', 64]))
        .optional(),
    },
    // 扩展信息字段
    {
      component: 'Select',
      fieldName: 'product_type',
      label: '货品类型',
      defaultValue: '',
      componentProps: {
        placeholder: '请选择货品类型',
        allowClear: true,
        options: useDictOptions('product_type'),
        class: 'w-1/3',
      },
      rules: 'selectRequired',
    },
    {
      component: 'Input',
      fieldName: 'name',
      label: '货品名称',
      componentProps: {
        placeholder: '请输入货品名称',
      },
      rules: z
        .string()
        .min(1, $t('ui.formRules.required', ['货品名称']))
        .max(255, $t('ui.formRules.maxLength', ['货品名称', 255])),
    },
    {
      component: 'Input',
      fieldName: 'spc',
      label: '货品规格',
      componentProps: {
        placeholder: '请输入货品规格',
      },
    },
    {
      component: 'Textarea',
      fieldName: 'description',
      label: '货品描述',
      componentProps: {
        placeholder: '请输入货品描述',
        allowClear: true,
        rows: 4,
      },
    },
    {
      component: 'Input',
      fieldName: 'barcode',
      label: '条形码',
      componentProps: {
        placeholder: '请输入条形码',
        class: 'w-2/3',
      },
      rules: z
        .string()
        .max(64, $t('ui.formRules.maxLength', ['条形码', 64]))
        .optional(),
    },
    {
      component: 'Input',
      fieldName: 'version',
      label: '版本号',
      componentProps: {
        placeholder: '请输入版本号',
        class: 'w-1/2',
      },
    },
    {
      component: 'Input',
      fieldName: 'category_id',
      label: '货品分类',
      componentProps: {
        placeholder: '请输入货品分类',
        allowClear: true,
        class: 'w-1/2',
      },
    },
    {
      component: 'Textarea',
      fieldName: 'remark',
      label: '备注',
      componentProps: {
        placeholder: '请输入备注',
        allowClear: true,
        rows: 4,
      },
    },
    // 库存管理字段
    {
      component: 'Select',
      fieldName: 'inventory_method',
      label: '库存方法',
      componentProps: {
        placeholder: '请选择库存方法',
        allowClear: true,
        options: useDictOptions('inventory_method'),
        class: 'w-1/2',
      },
    },
    {
      component: 'Select',
      fieldName: 'costing_method',
      label: '成本计算方法',
      componentProps: {
        placeholder: '请选择成本计算方法',
        allowClear: true,
        options: useDictOptions('costing_method'),
        class: 'w-1/2',
      },
    },
    {
      component: 'Switch',
      fieldName: 'is_batch_tracked',
      label: '是否批次跟踪',
      componentProps: {
        checkedChildren: '是',
        unCheckedChildren: '否',
      },
    },
    {
      component: 'Switch',
      fieldName: 'is_serial_tracked',
      label: '是否序列号跟踪',
      componentProps: {
        checkedChildren: '是',
        unCheckedChildren: '否',
      },
    },
    {
      component: 'InputNumber',
      fieldName: 'shelf_life_days',
      label: '保质期天数',
      componentProps: {
        placeholder: '请输入保质期天数',
        min: 0,
        class: 'w-1/3',
      },
    },
    {
      component: 'InputNumber',
      fieldName: 'safety_stock',
      label: '安全库存',
      componentProps: {
        placeholder: '请输入安全库存',
        min: 0,
        class: 'w-1/2',
      },
    },
    {
      component: 'InputNumber',
      fieldName: 'reorder_point',
      label: '再订货点',
      componentProps: {
        placeholder: '请输入再订货点',
        min: 0,
        class: 'w-1/2',
      },
    },
    {
      component: 'InputNumber',
      fieldName: 'max_stock',
      label: '最大库存',
      componentProps: {
        placeholder: '请输入最大库存',
        min: 0,
        class: 'w-1/2',
      },
    },
    {
      component: 'InputNumber',
      fieldName: 'default_warehouse_id',
      label: '默认仓库ID',
      componentProps: {
        placeholder: '请输入默认仓库ID',
        min: 0,
        class: 'w-1/2',
      },
    },
    {
      component: 'Input',
      fieldName: 'default_location',
      label: '默认库位',
      componentProps: {
        placeholder: '请输入默认库位',
        class: 'w-1/2',
      },
    },
    // 单位与转换字段
    {
      component: 'Select',
      fieldName: 'unit_pr',
      label: '采购计量单位',
      componentProps: {
        placeholder: '请选择采购计量单位',
        allowClear: true,
        options: useDictOptions('product_unit'),
        class: 'w-1/3',
      },
      rules: z
        .string()
        .min(1, $t('ui.formRules.required', ['采购计量单位']))
        .max(20, $t('ui.formRules.maxLength', ['采购计量单位', 20])),
    },
    {
      component: 'Select',
      fieldName: 'unit_wh',
      label: '库存计量单位',
      componentProps: {
        placeholder: '请选择库存计量单位',
        allowClear: true,
        options: useDictOptions('product_unit'),
        class: 'w-1/3',
      },
    },
    {
      component: 'Select',
      fieldName: 'unit_use',
      label: '使用计量单位',
      componentProps: {
        placeholder: '请选择使用计量单位',
        allowClear: true,
        options: useDictOptions('product_unit'),
        class: 'w-1/3',
      },
    },
    // 计量单位转换率 - 分解为独立的数字输入框，提升用户体验
    {
      component: 'InputNumber',
      fieldName: 'unit_rate_pr_wh',
      label: '采 → 仓转换率',
      componentProps: {
        placeholder: '采购单位到仓库单位的转换系数',
        min: 0,
        step: 0.01,
        class: 'w-1/4',
      },
      defaultValue: 1.0,
    },
    {
      component: 'InputNumber',
      fieldName: 'unit_rate_wh_use',
      label: '仓 → 用转换率',
      componentProps: {
        placeholder: '仓库单位到使用单位的转换系数',
        min: 0,
        step: 0.01,
        class: 'w-1/4',
      },
      defaultValue: 1.0,
    },
    {
      component: 'InputNumber',
      fieldName: 'unit_rate_pr_use',
      label: '采 → 用转换率',
      componentProps: {
        placeholder: '采购单位到使用单位的转换系数',
        min: 0,
        step: 0.01,
        class: 'w-1/4',
      },
      defaultValue: 1.0,
    },
    // 采购与成本字段
    {
      component: 'InputNumber',
      fieldName: 'preferred_supplier_id',
      label: '首选供应商ID',
      componentProps: {
        placeholder: '请输入首选供应商ID',
        min: 0,
        class: 'w-1/2',
      },
    },
    {
      component: 'InputNumber',
      fieldName: 'purchaser_id',
      label: '采购员ID',
      componentProps: {
        placeholder: '请输入采购员ID',
        min: 0,
        class: 'w-1/2',
      },
    },
    {
      component: 'InputNumber',
      fieldName: 'lead_time',
      label: '提前期',
      componentProps: {
        placeholder: '请输入提前期(天)',
        min: 0,
        class: 'w-1/2',
      },
    },
    {
      component: 'InputNumber',
      fieldName: 'moq',
      label: '最小起订量',
      componentProps: {
        placeholder: '请输入最小起订量',
        min: 0,
        class: 'w-1/2',
      },
    },
    {
      component: 'InputNumber',
      fieldName: 'last_purchase_price',
      label: '最近采购价',
      componentProps: {
        placeholder: '请输入最近采购价',
        min: 0,
        step: 0.01,
        class: 'w-1/2',
      },
    },
    {
      component: 'InputNumber',
      fieldName: 'list_price',
      label: '列表价',
      componentProps: {
        placeholder: '请输入列表价',
        min: 0,
        step: 0.01,
        class: 'w-1/2',
      },
    },
    {
      component: 'InputNumber',
      fieldName: 'standard_cost',
      label: '标准成本',
      componentProps: {
        placeholder: '请输入标准成本',
        min: 0,
        step: 0.01,
        class: 'w-1/2',
      },
    },
    {
      component: 'InputNumber',
      fieldName: 'current_cost',
      label: '当前成本',
      componentProps: {
        placeholder: '请输入当前成本',
        min: 0,
        step: 0.01,
        class: 'w-1/2',
      },
    },
    // 财务信息字段
    {
      component: 'Select',
      fieldName: 'tax_code',
      label: '税码',
      componentProps: {
        placeholder: '请选择税码',
        allowClear: true,
        options: useDictOptions('tax_code'),
        class: 'w-1/2',
      },
    },
    {
      component: 'InputNumber',
      fieldName: 'income_account_id',
      label: '收入科目ID',
      componentProps: {
        placeholder: '请输入收入科目ID',
        min: 0,
        class: 'w-1/2',
      },
    },
    {
      component: 'InputNumber',
      fieldName: 'cogs_account_id',
      label: '销售成本科目ID',
      componentProps: {
        placeholder: '请输入销售成本科目ID',
        min: 0,
        class: 'w-1/2',
      },
    },
    {
      component: 'InputNumber',
      fieldName: 'asset_account_id',
      label: '资产科目ID',
      componentProps: {
        placeholder: '请输入资产科目ID',
        min: 0,
        class: 'w-1/2',
      },
    },
  ];
}

// region 选项卡分类
/**
 * 获取按选项卡分类的产品表单配置
 * @description 为产品创建和编辑表单提供选项卡分类结构支持
 */
export interface TabbedSchemaItem {
  key: string;
  tab: string;
  fields: string[];
}

export function useTabbedSchema(): TabbedSchemaItem[] {
  return [
    {
      key: 'basic',
      tab: $t('product.product.tabBasicInfo'),
      fields: ['sku', 'product_type', 'name', 'spc', 'status', 'description'],
    },
    {
      key: 'extended',
      tab: $t('product.product.tabExtendedInfo'),
      fields: ['status', 'version', 'category_id', 'remark', 'barcode'],
    },
    {
      key: 'inventory',
      tab: $t('product.product.tabInventory'),
      fields: [
        'inventory_method',
        'costing_method',
        'is_batch_tracked',
        'is_serial_tracked',
        'shelf_life_days',
        'safety_stock',
        'reorder_point',
        'max_stock',
        'default_warehouse_id',
        'default_location',
      ],
    },
    {
      key: 'unit',
      tab: $t('product.product.tabUnitConversion'),
      fields: [
        'unit_pr',
        'unit_wh',
        'unit_use',
        'unit_rate_pr_wh',
        'unit_rate_wh_use',
        'unit_rate_pr_use',
      ],
    },
    {
      key: 'purchase',
      tab: $t('product.product.tabPurchaseCost'),
      fields: [
        'sku_supplier',
        'preferred_supplier_id',
        'purchaser_id',
        'lead_time',
        'moq',
        'last_purchase_price',
        'list_price',
        'standard_cost',
        'current_cost',
      ],
    },
    {
      key: 'financial',
      tab: $t('product.product.tabFinancialInfo'),
      fields: [
        'tax_code',
        'income_account_id',
        'cogs_account_id',
        'asset_account_id',
      ],
    },
  ];
}

// region 查询显示表格列
/**
 * 表格列配置
 * @description 使用函数的形式返回列数据而不是直接export一个Array常量，是为了响应语言切换时重新翻译表头
 * @param onActionClick 表格操作按钮点击事件
 */
export function useColumns(
  onActionClick?: (params: OnActionClickParams<ProductApi.Product>) => void,
): VxeTableGridOptions<ProductApi.Product>['columns'] {
  return [
    {
      title: '序',
      type: 'seq',
      width: 60,
      align: 'center',
      visible: true,
    },
    {
      field: 'sku',
      title: 'SKU',
      width: 120,
      align: 'left',
      showOverflow: 'tooltip',
      visible: true,
    },
    {
      field: 'name',
      title: '货品名称',
      minWidth: 200,
      align: 'left',
      showOverflow: 'tooltip',
      visible: true,
    },
    {
      field: 'spc',
      title: '货品规格',
      minWidth: 200,
      align: 'left',
      showOverflow: 'tooltip',
      visible: true,
    },
    {
      field: 'product_type',
      title: '货品类型',
      width: 100,
      align: 'left',
      showOverflow: 'tooltip',
      formatter: (row) => {
        const categoryMap = useDictOptions('product_type');
        return (
          categoryMap.find((item) => item.value === row.cellValue)?.label ||
          row.cellValue
        );
      },
      visible: true,
    },
    {
      field: 'unit_pr',
      title: '单位',
      width: 60,
      align: 'center',
      visible: true,
    },
    {
      field: 'status',
      title: '状态',
      width: 100,
      align: 'center',
      showOverflow: 'tooltip',
      formatter: dictFormatter('product_status'),
      visible: true,
    },
    {
      field: 'remark',
      title: '备注',
      width: 180,
      align: 'left',
      showOverflow: 'tooltip',
      visible: true,
    },
    {
      field: 'is_deleted',
      title: '是否删除',
      width: 100,
      align: 'center',
      showOverflow: 'tooltip',
      visible: false,
    },
    {
      align: 'center',
      cellRender: {
        attrs: {
          nameField: 'name',
          nameTitle: $t('product.product.title'),
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          op('product:product:edit', 'edit'),
          op('product:product:delete', 'delete'),
        ],
      },
      field: 'action',
      fixed: 'right',
      title: '操作',
      width: 120,
      visible: true,
    },
  ];
}
